<template>
  <div class="main-container">
    <el-form :model="query" inline label-width="80px">
      <el-form-item>
        <el-button v-permission="'category:save'" type="primary" @click="openFormModal()">添加分类</el-button>
        <el-button @click="resetQuery()">刷新</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="page.loading" :data="page.list" :tree-props="{ children: 'children' }" row-key="id">
      <el-table-column label="名称" prop="categoryName">
        <template #default="scope">
          <span>{{ scope.row.categoryName }}</span>
        </template>
      </el-table-column>
      <el-table-column :width="100" label="排序" prop="sort" />
      <el-table-column :width="100" label="状态">
        <template #default="scope">
          <enum-view :enum-name="'Status'" :enum-value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :width="220" fixed="right" label="操作" prop="address">
        <template #default="scope">
          <el-button v-permission="'category:save'" link type="primary" @click="openFormModal(null, scope.row.id)"> 添加</el-button>
          <el-divider direction="vertical" />
          <el-button v-permission="'category:edit'" link type="primary" @click="openFormModal(scope.row)">编辑</el-button>
          <el-divider direction="vertical" />
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-button link type="primary"
                >更多操作<el-icon class="el-icon--right"><arrow-down /></el-icon
              ></el-button>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <div @click="handleStatus(scope.row)">
                  <el-dropdown-item v-if="scope.row.status === 0">启用</el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.status === 1">禁用</el-dropdown-item>
                </div>
                <div @click="handleDelete(scope.row)">
                  <el-dropdown-item>删除</el-dropdown-item>
                </div>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <category-form ref="formRef" @refresh="handlePage" />
</template>
<script setup>
  import useTable from '@/utils/table.js'
  import { ref } from 'vue'
  import { commonApi } from '@/api/common'
  import CategoryForm from './CategoryForm.vue'
  import EnumView from '@/components/Enum/View/index.vue'
  import { ArrowDown } from '@element-plus/icons-vue'

  // 添加/修改
  const formRef = ref()
  const openFormModal = (item = null, parentId = '') => {
    formRef.value.onOpen(item, parentId)
  }

  // 基础功能
  const { page, handlePage, query, resetQuery, handleDelete, handleStatus } = useTable(
    {
      page: commonApi.categoryList,
      delete: commonApi.categoryDelete,
      status: commonApi.categoryUpdateStatus
    },
    {
      categoryType: 1
    }
  )
</script>
